import { Http, Headers, ConnectionBackend, XHRBackend, RequestOptions, RequestOptionsArgs, Request, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Router } from '@angular/router';
import { Injectable } from '@angular/core';
import 'rxjs/Rx';
declare var $: any;
@Injectable()
export class AuthHttp extends Http {
    private timeoutId: any;
    constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private router: Router) {
        super(backend, defaultOptions);
    }
    request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
        options = this.addHeader(url, options);
        return this.intercept(super.request(url, options));
    }
    get(url: string, options?: RequestOptionsArgs): Observable<Response> {
        options = this.addHeader(url, options);
        return this.intercept(super.get(url, options));
    }
    post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
        options = this.addHeader(url, options);
        return this.intercept(super.post(url, body, options));
    }
    put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
        options = this.addHeader(url, options);
        return this.intercept(super.put(url, body, options));
    }
    delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
        options = this.addHeader(url, options);
        return this.intercept(super.delete(url, options));
    }
    patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
        options = this.addHeader(url, options);
        return this.intercept(super.patch(url, body, options));
    }
    intercept(observable: Observable<Response>): Observable<Response> {
        return <Observable<Response>>observable.catch((error, source) => {
            console.log(error);
            if (error['_body']) {
                error = JSON.parse(error['_body']);
                if (this.timeoutId) {
                    clearTimeout(this.timeoutId);
                }
                this.timeoutId = setTimeout(() => {
                    alert(error.message);
                    if (error.message === 'session已失效') {
                        $('.modal').map(function() {
                            $(this).modal('hide');
                        });
                        this.router.navigate(['pages/login']);
                    }
                }, 1000);
            }
            return Observable.throw(error);
        });
    }
    addHeader(url: string | Request, options?: RequestOptionsArgs): RequestOptionsArgs {
        const gettoken = sessionStorage.getItem('Authentication');
        if (typeof url === 'string') { // meaning we have to add the token to the options, not in url
            if (!options) {
                options = new RequestOptions({});
                options.headers = new Headers();
            }
            if (!options.headers) {
                options.headers = new Headers();
            }
            if (!options.headers) {
                options.headers = new Headers();
            }
            options.headers.set('Authorization', gettoken);
        } else {
            url.headers.set('Authorization', gettoken);
        }
        return options;
    }
}
export function authHttpFactory(
    backend: XHRBackend,
    defaultOptions: RequestOptions,
    router: Router
) {
    return new AuthHttp(
        backend,
        defaultOptions,
        router
    );
};
export function authHttpProvider() {
    return {
        provide: Http,
        useFactory: authHttpFactory,
        deps: [
            XHRBackend,
            RequestOptions,
            Router
        ]
    };
};
